package com.wiloon.java8x.thread.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by wiloon on 4/27/17;
 */
public class LockTest {
    public static void main(String[] args) {
        final OutPutter1 output = new OutPutter1();
        new Thread() {
            public void run() {
                output.output("zhangsan");
            }
        }.start();
        new Thread() {
            public void run() {
                output.output("lisi");
            }
        }.start();
    }
}

class OutPutter1 {
    private Lock lock = new ReentrantLock();// 锁对象

    void output(String name) {
        // 线程输出方法
        lock.lock();// 得到锁
        try {
            for (int i = 0; i < name.length(); i++) {
                System.out.print(name.charAt(i));
            }
        } finally {
            lock.unlock();// 释放锁
        }
    }
}